const Jsonrpc = require('../util/jsonrpc2');

const jsonrpc = new Jsonrpc({
    test1(a) {
        console.log(a);
        return a;
    },
    test2(a, b) {
        console.log(a + b);
        return a + b;
    },
    test3(a) {
        console.log(this);
        return this;
    },
    test4: (o) => {
        //异常模拟
        return o / asdsad;
    },
    upload1() {
        return arguments;
    },
    upload2(o) {
        return o;
    }
}, true);


module.exports = function (ctx) {
    let data = ctx.request.body;
    if (ctx.request.header["content-type"].indexOf("multipart/form-data") >= 0) {
        params = JSON.parse(data.params);
        if (Array.isArray(params)) params.push(ctx.request.files);
        else if (params.constructor === Object) params.files = ctx.request.files;
        else params = [params, ctx.request.files];
        data.params = params;
    }
    ctx.body = jsonrpc.handle(data);
}
